//
//  HomeViewController.swift
//  GenericProjectForSwift
//
//  Created by 皮蛋菌 on 2023/5/14.
//  Copyright © 2023 漠然丶情到深处. All rights reserved.
//

import UIKit

class HomeViewController: LSBaseViewController,UITableViewDelegate,UITableViewDataSource {
    var videoListArray = [VideoModel]()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "视频播放器";
        // Do any additional setup after loading the view.
        analysis()
        createSubViews()
    }
    
    override func analysis() {
        var array = [[String:Any]]()
        
        let fileManager = FileManager.default
        let urls = fileManager.urls(for: .documentDirectory, in: .userDomainMask)
        let documentsDirectoryURL = urls[0]

        do {
            let fileURLs = try fileManager.contentsOfDirectory(at: documentsDirectoryURL, includingPropertiesForKeys: nil, options: .skipsHiddenFiles)

            for fileURL in fileURLs {
                var dic = [String:Any]()
                do {
                    let attributes = try fileManager.attributesOfItem(atPath: fileURL.path)
                    if let fileSize = attributes[.size] as? NSNumber {
                        let fileSizeInMB = fileSize.doubleValue / (1024.0 * 1024.0)

                        dic["videoUrlString"] = fileURL
                        dic["videoNameString"] = fileURL.deletingPathExtension().lastPathComponent
                        dic["videoSizeString"] = String(format: "%.2f MB", fileSizeInMB)
                    }
                } catch {
                    print("Error while getting file attributes: \(error.localizedDescription)")
                }
                array.append(dic)
            }
        } catch {
            print("Error while enumerating files: \(error.localizedDescription)")
        }
        
        videoListArray = [VideoModel].deserialize(from: array) as? [VideoModel] ?? []

    }
    
    override func createSubViews() {
        mainTableView.delegate = self
        mainTableView.dataSource = self
        mainTableView.register(HomeTableViewCell.self, forCellReuseIdentifier: "HomeTableViewCell")
        self.view.addSubview(self.mainTableView)
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return videoListArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "HomeTableViewCell")
        if cell == nil {
            cell = HomeTableViewCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: "HomeTableViewCell")
            cell?.selectionStyle = UITableViewCell.SelectionStyle.none
        }
        
        let contact: VideoModel = videoListArray[indexPath.row]
        
        if let contactCell = cell as? HomeTableViewCell {
            contactCell.configure(for: contact)
        }
        
        return cell!
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let contact: VideoModel = videoListArray[indexPath.row]
        
        let VideoPlayVC = VideoPlayerViewController.init()
        VideoPlayVC.modalPresentationStyle = UIModalPresentationStyle.fullScreen
        VideoPlayVC.videoURL = URL(string: contact.videoUrlString)
        self.present(VideoPlayVC, animated: true)
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return LSSYRealValue(value1: 100, value2: 200)
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return CGFLOAT_MIN
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return CGFLOAT_MIN
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
